package com.tanhua.dubbo.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;


import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.mapper.UserInfoMapper;

import com.tanhua.common.pojo.UserInfo;

import com.tanhua.common.vo.ErrorResult;

import com.tanhua.dubbo.server.api.CommentApi;

import com.tanhua.dubbo.server.pojo.Comment;

import com.tanhua.dubbo.server.vo.ManageCommentVo;
import com.tanhua.dubbo.server.vo.PageResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*查询评论列表方法*/
@Service
public class ManagerCommentService {
    @Reference
    private CommentApi commentApi;

    @Autowired
    private UserInfoMapper userInfoMapper;


    public PageResult queryCommentList(String publishId, Integer page, Integer pagesize, String sortProp, String sortOrder) {
        //设置分页
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        //查询评论列表,返回集合
        List<Comment> commentList = commentApi.queryCommentList(publishId, page, pagesize, sortProp, sortOrder);
        //统一异常处理
        if (ObjectUtil.isEmpty(commentList)) {
            ErrorResult.findCommentError();
        }
        //创建空集合存储ManageCommentVo对象
        List<ManageCommentVo> commentVoList = new ArrayList<>();
        //遍历获取到的评论集合,封装到ManageCommentVo对象中
        for (Comment comment : commentList) {
            ManageCommentVo messageCommentsVo = new ManageCommentVo();//创建vo对象
            messageCommentsVo.setContent(comment.getContent());//评论内容
            messageCommentsVo.setCreateDate(comment.getCreated());//评论时间
            messageCommentsVo.setId(Convert.toLong(new ObjectId()));//评论编号
            messageCommentsVo.setUserId(comment.getUserId());//评论人id
            pageResult.setCounts(commentList.size());//评论数量
            //查询UserInfo信息
            UserInfo userInfo = userInfoMapper.selectById(comment.getUserId());
            messageCommentsVo.setNickname(userInfo.getNickName());//昵称
            //添加videovo到集合中
            commentVoList.add(messageCommentsVo);
        }
        //封装集合信息
        pageResult.setItems(commentVoList);
        //返回结果
        return pageResult;
    }
}
